package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.Role;
import com.nondo.dean.auth.domain.RoleExample;
import com.nondo.dean.auth.mapper.RoleMapper;
import com.nondo.dean.auth.service.RoleService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linfeng
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role findByRoleId(Long roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public Role findByCode(String code) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(code);
        List<Role> roleList = roleMapper.selectByExample(example);
        if (roleList.isEmpty()) {
            return null;
        }
        return roleList.get(0);
    }

    @Override
    public List<Role> findByRoleIds(List<Long> ids) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        criteria.andDelFlagEqualTo(Role.DEL_FLAG_NORMAL);
        return roleMapper.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Role record) {
        Role oldRecord = roleMapper.selectByPrimaryKey(record.getId());
        if (oldRecord == null) {
            roleMapper.insertSelective(record);
        } else {
            roleMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        Role record = new Role();
        record.setId(id);
        record.setDelFlag(Role.DEL_FLAG_DELETE);
        roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Page<Role> findRolePage(Query<Role> query) {

        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        Role role = query.getData();
        if (role.getId() != null) {
            criteria.andIdEqualTo(role.getId());
        }
        if (StringUtils.isNotBlank(role.getName())) {
            criteria.andNameLike("'" + role.getName() + "%'");
        }
        if (StringUtils.isNotBlank(role.getCode())) {
            criteria.andCodeLike("'" + role.getCode() + "%'");
        }
        criteria.andDelFlagEqualTo(Role.DEL_FLAG_NORMAL);
        List<Role> resultList = roleMapper.selectByExampleWithRowbounds(example, new RowBounds(query.getOffset(), query.getLimit()));
        long total = roleMapper.countByExample(example);

        Page<Role> resultPage = Page.of(query);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }


}
